package com.zj.web.controller;
import com.zj.service.BannerService;
import com.zj.web.bean.BannerDTO;
import com.zj.web.bean.ResponseResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 轮播图控制器
 * */
@RestController
@RequestMapping("/banners")
@RefreshScope //开启动态刷新
@Tag(name = "轮播图控制器",description = "提供对轮播图资源的添加，查询并显示等操作")
public class BannerController {
    @Autowired
    private BannerService bannerService;

//    @Value("${product.pattern.dateFormat}")
//    private String dateFormat;

    @PostMapping("/add")
    @RefreshScope
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult> add(BannerDTO bannerDTO){

//        DateFormat df=new SimpleDateFormat(dateFormat);
//        System.out.println("今天是"+df.format(new Date()));

        BannerDTO banner =bannerService.addBanner(bannerDTO);
        return ResponseEntity.ok(ResponseResult.ok().setdata(banner));
    }

    /**
     * 前台：根据bannerid修改轮播图状态
     * */
    @PutMapping("/updateBannerStatus/{bannerId}/{bannerStatus}")
    @PreAuthorize("hasRole('ADMIN')")
    public ResponseEntity<ResponseResult> updateBannerStatus(@PathVariable("bannerId") Long bannerId,
                                                            @PathVariable("bannerStatus") Integer bannerStatus){
        BannerDTO bannerDTO = bannerService.updateBannerStatus(bannerId, bannerStatus);
        return ResponseEntity.ok(ResponseResult.ok().setdata(bannerDTO));
    }
}
